//
//  ListBootcamp_32.swift
//  SwiftUiExercise
//
//  Created by Mac on 2023/7/3.
//

import SwiftUI

struct ListBootcamp_32: View {
    
    @State var fruits:[String] = ["apple","orange","banana","peach"]
    
    var body: some View {
        NavigationView{
            List{
                Section(
                    header:
                        HStack{
                            Text("Fruits")
                            Image(systemName: "flame.fill")
                        }
                        .font(.headline)
                        .foregroundColor(.orange)
                ) {
                    ForEach(fruits,id: \.self) { fruit in
                        Text(fruit.capitalized)
                            .font(.caption)
                            .foregroundColor(.white)
                            .padding(.vertical)
//                            .frame(
//                                maxWidth:.infinity,
//                                maxHeight:.infinity
//                            )
//                            .background(Color.pink)
                    }
                    .onDelete { indexSet in
                        delete(indexSet: indexSet)
                    }
                    .onMove { indices, newOffset in
                        move(indices: indices, newOffset: newOffset)
                    }
                    .listRowBackground(Color.pink)
                }
             
            }
            .navigationTitle("Grocery List")
            .navigationBarItems(
                leading: EditButton(),
                trailing:AddButton
                   
            )
            .accentColor(.red)
            //.listStyle(.inset)
        }
   
        
      
    }
    
    var AddButton: some View{
        Button("Add",action:{
            add()
        })
    }
    
    func add(){
        fruits.append("Cocont")
    }
    
    func move(indices :IndexSet , newOffset :Int){
        fruits.move(fromOffsets: indices, toOffset: newOffset)
    }
    
    func delete(indexSet:IndexSet){
        fruits.remove(atOffsets: indexSet)
    }
}

struct ListBootcamp_32_Previews: PreviewProvider {
    static var previews: some View {
        ListBootcamp_32()
    }
}
